/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package com.aionemu.gameserver.network.aion.serverpackets;

import javolution.util.FastList;
import javolution.util.FastMap;

import com.aionemu.gameserver.dataholders.DataManager;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.PortalCooldown;
import com.aionemu.gameserver.model.team.alliance.PlayerAlliance;
import com.aionemu.gameserver.model.team.group.PlayerGroup;
import com.aionemu.gameserver.network.aion.AbstractAionServerPacket;
import com.aionemu.gameserver.network.aion.GameClient;

/**
 * @author MrPoke
 *
 */
public class SM_INSTANCE_INFO extends AbstractAionServerPacket<GameClient>
{
	private Player	player;
	private boolean	update;
	private boolean	enter;
	private int		instanceId;

	public SM_INSTANCE_INFO(Player player, boolean update, boolean enter)
	{
		this.player = player;
		this.update = update;
		this.enter = enter;
		this.instanceId = 0;
	}

	public SM_INSTANCE_INFO(Player player, int instanceId)
	{
		this.player = player;
		this.update = true;
		this.enter = true;
		this.instanceId = instanceId;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void writeImpl(GameClient client)
	{
		if (!enter && !update)
		{
			writeH(0x2);
			writeD(0x0);
			writeH(0x1);
			writeD(player.getObjectId());
			writeH(0x0);
			writeS(player.getName());
			return;
		}
		if (instanceId != 0)
		{
			writeC(update ? 2 : 1);
			writeC(enter ? 3 : 0);
			writeD(0x0);
			writeH(1);
			writeD(player.getObjectId());
			writeH(1);
			writeD(DataManager.INSTANCE_COOLTIME_DATA.getInstanceCooltimeByWorldId(instanceId).getId());
			int objId = 0;
			objId = player.getPlayerGroup() != null ? player.getPlayerGroup().getObjectId() : 0;
			objId = player.getPlayerAlliance() != null ? player.getPlayerAlliance().getObjectId() : 0;
			writeD(objId);
			writeD((int) (player.getPortalCoolDown(instanceId) - System.currentTimeMillis()) / 1000);
			writeS(player.getName());
			return;
		}
		FastList<Player> players = FastList.newInstance();
		PlayerGroup playerGroup = player.getPlayerGroup();
		PlayerAlliance playerAlliance = player.getPlayerAlliance();
		if (playerGroup != null)
		{
			for (Player member : playerGroup.getOnlineMembers())
			{
				if (member.isOnline() && !member.getPortalCoolDowns().isEmpty())
					players.add(member);
			}
		}
		else if (playerAlliance != null)
		{
			for (Player member : playerAlliance.getOnlineMembers())
			{
				if (member.isOnline() && !member.getPortalCoolDowns().isEmpty())
					players.add(member);
			}
		}
		else
		{
			if (!player.getPortalCoolDowns().isEmpty())
				players.add(player);
		}

		if (players.isEmpty())
		{
			writeD(0x0);
			writeD(0x0);
		}
		else
		{
			writeC(update ? 2 : 1);
			writeC(enter ? 3 : 0);
			writeD(0x0);
			writeH(players.size());
			{
				for (Player member : players)
				{
					writeD(member.getObjectId());
					writeH(member.getPortalCoolDowns().size());
					for (FastMap.Entry<Integer, PortalCooldown> e = member.getPortalCoolDowns().head(), end = member.getPortalCoolDowns().tail(); (e = e
							.getNext()) != end;)
					{
						writeD(DataManager.INSTANCE_COOLTIME_DATA.getInstanceCooltimeByWorldId(e.getKey()).getId());
						writeD(0x0);
						writeD((int) (e.getValue().getReuseTime() - System.currentTimeMillis()) / 1000);
						writeS(member.getName());
					}
				}
			}
		}
		FastList.recycle(players);
	}
}
